//
//  BK_Enviroment.m
//  Bespoke
//
//  Created by Rafale Chang on 12/7/23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BK_Enviroment.h"

#define BESPOKEUSER @"BespokeUser"
#define BESPOKEPASS @"BespokePass"
#define BESPOKEFULLNAME @"BespokeFullName"
#define BESPOKEPARTNERNAME @"BespokePartnerName"
#define BESPOKEPARTNEREMAIL @"BespokePartnerEmail"
#define BESPOKEYOURPHOTO @"BespokeYourPhoto"
#define BESPOKEPLANERNAME @"BespokePlanerName"
#define BESPOKEPLANEREMAIL @"BespokePlanerEmail"
#define FACEBOOKID @"FaceBookID"
#define FACEBOOKTOKEN @"FaceBookToken"
#define FACEBOOKFEEDCHECK @"FacebookFeedCheck"
#define OAUTHKEY @"OauthKey"
#define OAUTHSECRET @"OauthSecret"
#define TWITTERFEEDCHECK @"TwitterFeedCheck"
#define FIRSTUSECHECK @"FirstUseCheck"
#define TUMBLRBLOGNAME @"TumblrBlogName"
#define TUMBLRFEEDCHECK @"TumblrSwitchCheck"
#define INSTAGRAMTOKEN @"InstagramToken"
#define INSTAGRAMFEEDCHECK @"InstagramFeedCheck"
#define GOOGLEREADERLINK @"GoogleReaderWebLink"
#define GOOGLEREADERTITLE @"GoogleReaderTitle"
#define GOOGLEREADERCONTENT @"GoogleReaderContent"
#define GOOGLESWITCHCHECK @"GoogleSwitchCheck"
#define CHANNEL @"Channel"
#define BUDGET @"Budget"
#define CALENDAR @"Calendar"

@implementation BK_Enviroment

@synthesize userName, passWord, fullName, partnerName;
@synthesize yourPhoto, PlanerName , PlanerEmail;
@synthesize NS_OauthKey,NS_OauthSecret , NS_FaceBookID, NS_FaceBookToken, NS_InstagramToken, FacebookFeedCheck;
@synthesize Array_GoogleReaderLink,Array_GoogleSwitchCheck,Array_GoogleTitleSelected,Array_GoogleContentSelected,NS_Tumblr;
@synthesize partnerEmail, NS_Channel, NS_Budget, NS_Calendar, TumblrFeedCheck, TwitterFeedCheck, InstagramFeedCheck;
@synthesize NonFirstUseCheck;


-(id)init
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    if([prefs objectForKey:FIRSTUSECHECK] != nil)
        NonFirstUseCheck = [prefs boolForKey:FIRSTUSECHECK];
    
    if([prefs stringForKey:BESPOKEUSER].length > 0)
        userName = [prefs stringForKey:BESPOKEUSER];
    
    if([prefs stringForKey:BESPOKEPASS].length > 0)
        passWord = [prefs stringForKey:BESPOKEPASS];
    
    if([prefs stringForKey:BESPOKEFULLNAME].length > 0)
        fullName = [prefs stringForKey:BESPOKEFULLNAME];

    if([prefs stringForKey:BESPOKEPARTNERNAME].length > 0)
        partnerName = [prefs stringForKey:BESPOKEPARTNERNAME];
    
    if([prefs stringForKey:BESPOKEPARTNEREMAIL].length > 0)
    {
        partnerEmail = [prefs stringForKey:BESPOKEPARTNEREMAIL];
    }
    
    if([prefs objectForKey:BESPOKEYOURPHOTO] != nil)
    {
        yourPhoto = [UIImage imageWithData:[prefs objectForKey:BESPOKEYOURPHOTO]];
    }
    
    if([prefs objectForKey:BESPOKEPLANERNAME] != nil)
    {
        PlanerName = [prefs stringForKey:BESPOKEPLANERNAME];
    }
    
    if([prefs objectForKey:BESPOKEPLANEREMAIL] != nil)
    {
        PlanerEmail = [prefs stringForKey:BESPOKEPLANEREMAIL];
    }
    
    if([prefs objectForKey:FACEBOOKID] != nil)
    {
        NS_FaceBookID = [prefs stringForKey:FACEBOOKID];
    }
    if([prefs objectForKey:FACEBOOKTOKEN] != nil)
    {
        NS_FaceBookToken = [prefs stringForKey:FACEBOOKTOKEN];
    }
    if([prefs objectForKey:FACEBOOKFEEDCHECK] != nil)
    {
        FacebookFeedCheck = [prefs boolForKey:FACEBOOKFEEDCHECK];
    }
    if([prefs objectForKey:OAUTHKEY] != nil)
    {
        NS_OauthKey = [prefs stringForKey:OAUTHKEY];
    }
    if([prefs objectForKey:OAUTHSECRET] != nil)
    {
        NS_OauthSecret = [prefs stringForKey:OAUTHSECRET];
    }
    if([prefs objectForKey:TWITTERFEEDCHECK] != nil)
    {
        TwitterFeedCheck = [prefs boolForKey:TWITTERFEEDCHECK];
    }
    
    if([prefs objectForKey:TUMBLRBLOGNAME] != nil)
    {
        NS_Tumblr = [prefs stringForKey:TUMBLRBLOGNAME];
    }
    if([prefs objectForKey:TUMBLRFEEDCHECK] != nil)
    {
        TumblrFeedCheck = [prefs boolForKey:TUMBLRFEEDCHECK];
    }
    
    if([prefs objectForKey:INSTAGRAMTOKEN] != nil)
    {
        NS_InstagramToken = [prefs stringForKey:INSTAGRAMTOKEN];
    }
    if([prefs objectForKey:INSTAGRAMFEEDCHECK] != nil)
    {
        InstagramFeedCheck = [prefs boolForKey:INSTAGRAMFEEDCHECK];
    }
    
    if([prefs objectForKey:GOOGLEREADERLINK] != nil)
    {
        Array_GoogleTitleSelected = [prefs mutableArrayValueForKey:GOOGLEREADERTITLE];
        Array_GoogleContentSelected = [prefs mutableArrayValueForKey:GOOGLEREADERCONTENT];
        Array_GoogleReaderLink = [prefs mutableArrayValueForKey:GOOGLEREADERLINK];
        Array_GoogleSwitchCheck = [prefs mutableArrayValueForKey:GOOGLESWITCHCHECK];
    }
    else if([prefs objectForKey:GOOGLEREADERLINK] == nil)
    {
        Array_GoogleTitleSelected = [[NSMutableArray alloc] init];
        Array_GoogleContentSelected = [[NSMutableArray alloc] init];
        Array_GoogleReaderLink = [[NSMutableArray alloc] init];
        Array_GoogleSwitchCheck = [[NSMutableArray alloc] init];
    }
    
    if([prefs objectForKey:CHANNEL] != nil) {
        NS_Channel = [prefs objectForKey:CHANNEL];
    }
    
    if([prefs objectForKey:BUDGET] != nil) {
        self.NS_Budget = [prefs objectForKey:BUDGET];
    }
    
    if([prefs objectForKey:BUDGET] != nil) {
        self.NS_Calendar = [prefs objectForKey:CALENDAR];
    }

    return self;
}

-(void)setNonFirstCheck:(BOOL)Check {
    
    self.NonFirstUseCheck = Check;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:self.NonFirstUseCheck forKey:FIRSTUSECHECK];
    [prefs synchronize];
}

-(void)setAuthenticationUserName:(NSString *)usr Password:(NSString *)pwd
{
    passWord = pwd;
    userName = usr;
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:userName forKey:BESPOKEUSER];
    [prefs setObject:passWord forKey:BESPOKEPASS];
    [prefs synchronize];
}

-(void)setPlanerInfo:(NSString *)planner Email:(NSString *)planEmail
{
    PlanerName = planner;
    PlanerEmail = planEmail;
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:PlanerName forKey:BESPOKEPLANERNAME];
    [prefs setObject:PlanerEmail forKey:BESPOKEPLANEREMAIL];
    [prefs synchronize];
}

-(void)setAccountInfoFullName:(NSString *)fName
                   setPartner:(NSString *)pName
{
    fullName = fName;
    partnerName = pName;
    NSLog(@"FullName=%@", fullName);
    NSLog(@"PartnerName=%@", partnerName);
    
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:fullName forKey:BESPOKEFULLNAME];
    [prefs setObject:partnerName forKey:BESPOKEPARTNERNAME];
    [prefs synchronize];
}

-(void)SetPartner:(NSString *)Name SetEmail:(NSString *)Email {
    
    partnerName = Name;
    partnerEmail = Email;
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:partnerName forKey:BESPOKEPARTNERNAME];
    [prefs setObject:partnerEmail forKey:BESPOKEPARTNEREMAIL];
    [prefs synchronize];
}

-(void)setAccountYourImage:(UIImage *)yPhoto {
    yourPhoto = yPhoto;
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData *imageData = UIImagePNGRepresentation(yourPhoto);
    [prefs setObject:imageData forKey:BESPOKEYOURPHOTO];
    [prefs synchronize];
}

-(void)setAccountInfoFaceBookID:(NSString *)sFaceBookID
               setFaceBookTotken:(NSString *)sFaceBookToken
{
    NS_FaceBookID = sFaceBookID;
    NS_FaceBookToken = sFaceBookToken;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:NS_FaceBookID forKey:FACEBOOKID];
    [prefs setObject:NS_FaceBookToken forKey:FACEBOOKTOKEN];
    [prefs synchronize];
}

-(void)setFacebookONorOff:(BOOL)FacebookCheck {
    
    self.FacebookFeedCheck = FacebookCheck;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:self.FacebookFeedCheck forKey:FACEBOOKFEEDCHECK];
    [prefs synchronize];
}

-(void)setAccountInfoTwitterKey:(NSString *)oauth_token
               setTwitterSecret:(NSString *)oauth_token_secret
{
    NS_OauthKey = oauth_token;
    NS_OauthSecret = oauth_token_secret;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:NS_OauthKey forKey:OAUTHKEY];
    [prefs setObject:NS_OauthSecret forKey:OAUTHSECRET];
    [prefs synchronize];
}

-(void)setTwitterONorOff:(BOOL)TwitterCheck {
    
    self.TwitterFeedCheck = TwitterCheck;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:self.TwitterFeedCheck forKey:TWITTERFEEDCHECK];
    [prefs synchronize];
}

-(void)setAccountInfoTumblr:(NSString *)TumblrSwitchCheck {
    NS_Tumblr = TumblrSwitchCheck;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:NS_Tumblr forKey:TUMBLRBLOGNAME];
    [prefs synchronize];
}

-(void)setTumblrONorOff:(BOOL)TumblrCheck {
    
    self.TumblrFeedCheck = TumblrCheck;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:self.TumblrFeedCheck forKey:TUMBLRFEEDCHECK];
    [prefs synchronize];
}

- (void)setAccountInfoInstagram:(NSString *)InstagramToken {
    NS_InstagramToken = InstagramToken;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:NS_InstagramToken forKey:INSTAGRAMTOKEN];
    [prefs synchronize];
}

-(void)setInstagramONorOff:(BOOL)InstagramCheck {
    
    self.InstagramFeedCheck = InstagramCheck;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setBool:self.InstagramFeedCheck forKey:INSTAGRAMFEEDCHECK];
    [prefs synchronize];
}

-(void)setAccountInfoGoogleReader:(NSString *)GoogleTitle SetContent:(NSString *)GoogleContent SetWebLink:(NSString *)GoogleLink {
    [Array_GoogleTitleSelected addObject:GoogleTitle];
    [Array_GoogleContentSelected addObject:GoogleContent];
    [Array_GoogleReaderLink addObject:GoogleLink];
    [Array_GoogleSwitchCheck addObject:[NSNumber numberWithInteger:1]];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    [prefs setObject:Array_GoogleTitleSelected forKey:GOOGLEREADERTITLE];
    [prefs setObject:Array_GoogleContentSelected forKey:GOOGLEREADERCONTENT];
    [prefs setObject:Array_GoogleReaderLink forKey:GOOGLEREADERLINK];
    [prefs setObject:Array_GoogleSwitchCheck forKey:GOOGLESWITCHCHECK];
    [prefs synchronize];
}

-(void)setGoogleSwitchCheck:(int)GoogleSwitchCheck SetIndex:(int)Index {
    [Array_GoogleSwitchCheck replaceObjectAtIndex:Index withObject:[NSNumber numberWithInteger:GoogleSwitchCheck]];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:Array_GoogleSwitchCheck forKey:GOOGLESWITCHCHECK];
    [prefs synchronize];
}

-(void)setGoogleDelete:(int)Index {
    [Array_GoogleTitleSelected removeObjectAtIndex:Index];
    [Array_GoogleContentSelected removeObjectAtIndex:Index];
    [Array_GoogleReaderLink removeObjectAtIndex:Index];
    [Array_GoogleSwitchCheck removeObjectAtIndex:Index];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    [prefs setObject:Array_GoogleTitleSelected forKey:GOOGLEREADERTITLE];
    [prefs setObject:Array_GoogleContentSelected forKey:GOOGLEREADERCONTENT];
    [prefs setObject:Array_GoogleReaderLink forKey:GOOGLEREADERLINK];
    [prefs setObject:Array_GoogleSwitchCheck forKey:GOOGLESWITCHCHECK];
    [prefs synchronize];
}

-(void)setAccountChannel:(NSString *)Channel {
    self.NS_Channel = Channel;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:NS_Channel forKey:CHANNEL];
    [prefs synchronize];
}

-(void)setAccountYourBudget:(NSString *)Budget {
    self.NS_Budget = Budget;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:self.NS_Budget forKey:BUDGET];
    [prefs synchronize];
}

-(void)setAccountYourCalendar:(NSString *)Calendar {
    self.NS_Calendar = Calendar;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:self.NS_Calendar forKey:CALENDAR];
    [prefs synchronize];
}

-(void)clearEnviroment
{
    userName = nil;
    passWord = nil;
    fullName = nil;
    partnerName = nil;
    partnerEmail = nil;
    PlanerName = nil;
    PlanerEmail = nil;
    yourPhoto = nil;
    NS_OauthKey = nil;
    NS_OauthSecret = nil;
    NS_FaceBookID = nil;
    NS_FaceBookToken = nil;
    NS_Tumblr = nil;
    NS_InstagramToken = nil;
    NS_Channel = nil;
    NS_Budget = nil;
    NS_Calendar = nil;
    FacebookFeedCheck = NO;
    TwitterFeedCheck = NO;
    TumblrFeedCheck = NO;
    InstagramFeedCheck = NO;
    
    
    Array_GoogleTitleSelected = [[NSMutableArray alloc] init];
    Array_GoogleContentSelected = [[NSMutableArray alloc] init];
    Array_GoogleReaderLink = [[NSMutableArray alloc] init];
    Array_GoogleSwitchCheck = [[NSMutableArray alloc] init];
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs removeObjectForKey:BESPOKEFULLNAME];
    [prefs removeObjectForKey:BESPOKEPASS];
    [prefs removeObjectForKey:BESPOKEUSER];
    [prefs removeObjectForKey:BESPOKEPARTNERNAME];
    [prefs removeObjectForKey:BESPOKEPARTNEREMAIL];
    [prefs removeObjectForKey:BESPOKEPLANERNAME];
    [prefs removeObjectForKey:BESPOKEPLANEREMAIL];
    [prefs removeObjectForKey:BESPOKEYOURPHOTO];
    
    [prefs removeObjectForKey:FACEBOOKID];
    [prefs removeObjectForKey:FACEBOOKTOKEN];
    [prefs removeObjectForKey:FACEBOOKFEEDCHECK];
    
    [prefs removeObjectForKey:OAUTHKEY];
    [prefs removeObjectForKey:OAUTHSECRET];
    [prefs removeObjectForKey:TWITTERFEEDCHECK];
    
    [prefs removeObjectForKey:TUMBLRBLOGNAME];
    [prefs removeObjectForKey:TUMBLRFEEDCHECK];
    
    [prefs removeObjectForKey:INSTAGRAMTOKEN];
    [prefs removeObjectForKey:INSTAGRAMFEEDCHECK];
    
    [prefs removeObjectForKey:GOOGLEREADERTITLE];
    [prefs removeObjectForKey:GOOGLEREADERCONTENT];
    [prefs removeObjectForKey:GOOGLEREADERLINK];
    [prefs removeObjectForKey:GOOGLESWITCHCHECK];
    [prefs  synchronize];
}

@end
